package com.liao.controller;

import com.liao.common.R;
import com.liao.po.Goods;
import com.liao.po.Goodstype;
import com.liao.service.GoodsService;
import com.liao.service.GoodstypeService;
import com.liao.utils.PageUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodstypeService goodstypeService;

    /**
     * 根据参数获取用户列表
     */
    @GetMapping("/list")
    @ApiOperation("根据参数获取用户列表")
    //@RequiresPermissions("product:skuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPageByCodition(params);

        return R.success(page);
    }


    /**
     * 保存
     */
    @PostMapping("/save")
    public R saveOrUpdate(@RequestBody Goods goods){
        LocalDateTime creatdate = goods.getCreatdate();

        Integer tid = goods.getTid();
        Goodstype byId = goodstypeService.getById(tid);

//        if (byId == null){
//            return R.error("商品类型ID必须存在");
//        }



        if (creatdate == null){
            goods.setCreatdate(LocalDateTime.now());
        }
        goodsService.saveOrUpdate(goods);
        return R.success("保存成功");
    }

    /**
     * 修改
     */
//    @RequestMapping("/update")
//    //@RequiresPermissions("product:skuinfo:update")
//    public R update(@RequestBody Goods goods){
//        goodsService.updateById(goods);
//        return R.success("修改成功");
//    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:skuinfo:delete")
    public R delete(@RequestBody Long[] skuIds){
        goodsService.removeByIds(Arrays.asList(skuIds));

        return R.success("删除成功");
    }
}
